import java.util.Objects;

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode left = head;
        ListNode right = head;

        while (n > 0) {
            n--;
            if (Objects.isNull(right.next)) {
                head = head.next;
                return head;
            }
            right = right.next;
        }

        while (Objects.nonNull(right.next)) {
            right = right.next;
            left = left.next;
        }

        left.next = left.next.next;
        return head;
    }
}